/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.delivery.multishop.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.BmapPoint;
import com.yami.shop.delivery.common.model.SameCity;
import com.yami.shop.delivery.common.service.SameCityService;
import com.yami.shop.security.multishop.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;


/**
 * @author lhd
 * @date 2020-05-23 08:26:25
 */
@RestController
@RequestMapping("/delivery/sameCity")
@Api(tags = "商家端同城配送接口")
public class SameCityController {

    @Autowired
    private SameCityService sameCityService;

    @GetMapping("/getSameCityInfo")
    @ApiOperation(value = "获取同城配送数据")
    public ServerResponseEntity<SameCity> getSameCityPage() {
        SameCity sameCity = sameCityService.getSameCityByShopId(SecurityUtils.getShopUser().getShopId());
        if (Objects.nonNull(sameCity)) {
            JSONArray jsonArray = JSONUtil.parseArray(sameCity.getPositionIds());
            sameCity.setPolygonPath(jsonArray.toList(BmapPoint.class));
        }
        return ServerResponseEntity.success(sameCity);
    }

    @PostMapping
    @ApiOperation(value = "新增同城配送数据")
    public ServerResponseEntity<Boolean> save(@RequestBody @Valid SameCity sameCity) {
        SameCity sameCityDb = sameCityService.getOne(new LambdaQueryWrapper<SameCity>().eq(SameCity::getShopId, SecurityUtils.getShopUser().getShopId()));
        JSONArray jsonArray = new JSONArray(sameCity.getPolygonPath());
        sameCity.setPositionIds(jsonArray.toString());
        sameCity.setShopId(SecurityUtils.getShopUser().getShopId());
        if (Objects.nonNull(sameCityDb)) {
            sameCityService.updateById(sameCity);
        } else {
            sameCityService.save(sameCity);
        }
        sameCityService.removeSameCityByShopId(SecurityUtils.getShopUser().getShopId());
        return ServerResponseEntity.success();
    }

    @PutMapping
    @ApiOperation(value = "修改同城配送数据")
    public ServerResponseEntity<Boolean> updateById(@RequestBody @Valid SameCity sameCity) {
        JSONArray jsonArray = new JSONArray(sameCity.getPolygonPath());
        sameCity.setPositionIds(jsonArray.toString());
        sameCity.setShopId(SecurityUtils.getShopUser().getShopId());
        boolean flag = sameCityService.updateById(sameCity);
        sameCityService.removeSameCityByShopId(SecurityUtils.getShopUser().getShopId());
        return ServerResponseEntity.success(flag);
    }

    @DeleteMapping("/{samecityId}")
    @ApiOperation(value = "根据id删除同城配送数据")
    @ApiImplicitParam(name = "samecityId", value = "同城配送id", required = true, dataType = "Long")
    public ServerResponseEntity<Boolean> removeById(@PathVariable Long samecityId) {
        boolean flag = sameCityService.removeById(samecityId);
        sameCityService.removeSameCityByShopId(SecurityUtils.getShopUser().getShopId());
        return ServerResponseEntity.success(flag);
    }
}
